package com.insuremonitor.system.service.impl;

import com.insuremonitor.common.core.domain.AjaxResult;
import com.insuremonitor.system.domain.*;
import com.insuremonitor.system.mapper.*;
import com.insuremonitor.system.service.IFrontWarrantyInsureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 保险投保信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class FrontWarrantyInsureServiceImpl implements IFrontWarrantyInsureService 
{
    @Autowired
    private FrontWarrantyInsureMapper frontWarrantyInsureMapper;

    @Autowired
    private FrontInsureRateExtendMapper frontInsureRateExtendMapper;

    @Autowired
    private FrontInsureRateGoodsMapper frontInsureRateGoodsMapper;

    @Autowired
    private FrontInsureRateKindMapper frontInsureRateKindMapper;

    @Autowired
    private FrontInsureTemplateMapper frontInsureTemplateMapper;

    @Autowired
    private FrontReportInsureProcessMapper frontReportInsureProcessMapper;

    @Autowired
    private  FrontReportInsureMapper frontReportInsureMapper;

    @Autowired
    private  FrontReportInsureAccountMapper frontReportInsureAccountMapper;


    /**
     * 查询保险投保信息
     * 
     * @param warrantyId 保险投保信息主键
     * @return 保险投保信息
     */
    @Override
    public FrontWarrantyInsure selectFrontWarrantyInsureByWarrantyId(Long warrantyId)
    {
        FrontWarrantyInsure frontWarrantyInsure = frontWarrantyInsureMapper.selectFrontWarrantyInsureByWarrantyId(warrantyId);
        if(frontWarrantyInsure!=null){

            if(frontWarrantyInsure.getInsureExtendIds()!=null && !"".equals(frontWarrantyInsure.getInsureExtendIds()) ){
                String[] InsureExtendIds = frontWarrantyInsure.getInsureExtendIds().split(",");
                if(InsureExtendIds.length>0){
                    List<FrontInsureRateExtend> frontInsureRateExtendLis=new ArrayList<FrontInsureRateExtend>();
                    for(String extendId:InsureExtendIds){
                        FrontInsureRateExtend insureRateExtend = frontInsureRateExtendMapper.selectFrontInsureRateExtendByInsureExtendId(Long.valueOf(extendId));
                        frontInsureRateExtendLis.add(insureRateExtend);
                    }
                    frontWarrantyInsure.setInsureRateExtends(frontInsureRateExtendLis);
                }
            }


            if(frontWarrantyInsure.getInsureKindId()!=null  && !"".equals(frontWarrantyInsure.getInsureKindId())){
                FrontInsureRateKind insureRateKind = frontInsureRateKindMapper.selectFrontInsureRateKindByInsureKindId(frontWarrantyInsure.getInsureKindId());
                if(insureRateKind!=null){
                    FrontInsureRateGoods frontInsureRateGoods = frontInsureRateGoodsMapper.selectFrontInsureRateGoodsByInsureGoodsId(frontWarrantyInsure.getInsureGoodsId());
                    insureRateKind.setInsureRateGoods(frontInsureRateGoods);
                }
                frontWarrantyInsure.setInsureRateKind(insureRateKind);
            }

            FrontInsureTemplate frontInsureTemplate = frontInsureTemplateMapper.selectFrontInsureTemplateByTemplateCode(frontWarrantyInsure.getTemplateCode());
            if(frontInsureTemplate!=null && frontInsureTemplate.getTemplateName()!=null ){
                frontWarrantyInsure.setTemplateName(frontInsureTemplate.getTemplateName());
            }

            //个人理赔账号
            FrontReportInsureAccount frontReportInsureAccount = frontReportInsureAccountMapper.selectFrontReportInsureAccountByOrderNum(frontWarrantyInsure.getOrderNum());
            if(frontReportInsureAccount!=null){
                frontWarrantyInsure.setFrontReportInsureAccount(frontReportInsureAccount);
            }

            //个人理赔原因
            FrontReportInsure frontReportInsure = frontReportInsureMapper.selectFrontReportInsureByOrderNum(frontWarrantyInsure.getOrderNum());
            if(frontReportInsure!=null){
                frontWarrantyInsure.setFrontReportInsure(frontReportInsure);
            }




        }
        return frontWarrantyInsure;
    }

    /**
     * 查询保险投保信息列表
     * 
     * @param frontWarrantyInsure 保险投保信息
     * @return 保险投保信息
     */
    @Override
    public List<FrontWarrantyInsure> selectFrontWarrantyInsureList(FrontWarrantyInsure frontWarrantyInsure)
    {
        frontWarrantyInsure.setStatus("1");
        frontWarrantyInsure.setOrderStatus("3"); //订单成功
        frontWarrantyInsure.setReportStatus("2"); //立案状态
        List<FrontWarrantyInsure> list = frontWarrantyInsureMapper.selectFrontWarrantyInsureList(frontWarrantyInsure);
        for (FrontWarrantyInsure frontWarranty:list){
            //添加保险名称
            FrontInsureTemplate frontInsureTemplate = frontInsureTemplateMapper.selectFrontInsureTemplateByTemplateCode(frontWarranty.getTemplateCode());
            if(frontInsureTemplate!=null && frontInsureTemplate.getTemplateName()!=null ){
                frontWarranty.setTemplateName(frontInsureTemplate.getTemplateName());
            }
            //添加
            if(frontWarranty.getInsureExtendIds()!=null && !"".equals(frontWarranty.getInsureExtendIds())){
                String[] InsureExtendIds = frontWarranty.getInsureExtendIds().split(",");
                if(InsureExtendIds.length>0){
                    List<FrontInsureRateExtend> frontInsureRateExtendLis=new ArrayList<FrontInsureRateExtend>();
                    for(String extendId:InsureExtendIds){
                        FrontInsureRateExtend insureRateExtend = frontInsureRateExtendMapper.selectFrontInsureRateExtendByInsureExtendId(Long.valueOf(extendId));
                        frontInsureRateExtendLis.add(insureRateExtend);
                    }
                    frontWarranty.setInsureRateExtends(frontInsureRateExtendLis);
                }
            }

            if(frontWarranty.getInsureKindId()!=null && !"".equals(frontWarranty.getInsureKindId())  ){
                FrontInsureRateKind insureRateKind = frontInsureRateKindMapper.selectFrontInsureRateKindByInsureKindId(frontWarranty.getInsureKindId());
                if(insureRateKind!=null){
                    FrontInsureRateGoods frontInsureRateGoods = frontInsureRateGoodsMapper.selectFrontInsureRateGoodsByInsureGoodsId(frontWarrantyInsure.getInsureGoodsId());
                    insureRateKind.setInsureRateGoods(frontInsureRateGoods);
                }
                frontWarranty.setInsureRateKind(insureRateKind);
            }

            //个人理赔账号
            FrontReportInsureAccount frontReportInsureAccount = frontReportInsureAccountMapper.selectFrontReportInsureAccountByOrderNum(frontWarranty.getOrderNum());
            if(frontReportInsureAccount!=null){
                frontWarranty.setFrontReportInsureAccount(frontReportInsureAccount);
            }

            //个人理赔原因
            FrontReportInsure frontReportInsure = frontReportInsureMapper.selectFrontReportInsureByOrderNum(frontWarranty.getOrderNum());
            if(frontReportInsure!=null){
                frontWarranty.setFrontReportInsure(frontReportInsure);
            }

            //赔付过程
            List<FrontReportInsureProcess> frontReportInsureProcesses = frontReportInsureProcessMapper.selectFrontReportInsureProcessByOrderNum(frontWarranty.getOrderNum());
            if(frontReportInsureProcesses!=null){
                frontWarranty.setFrontReportInsureProcesses(frontReportInsureProcesses);
            }
//            if(frontReportInsureProcess!=null){
//                if("0".equals(frontReportInsureProcess.getSuccessStatus())){
//                    frontWarranty.setRollBack("1");
//                    frontWarranty.setRollRemark(frontReportInsureProcess.getRemark());
//                }else{
//                    frontWarranty.setRollBack("0");
//                }
//            }


        }
        return list;
    }

    @Override
    @Transactional
    public AjaxResult upInfo(UpInfoVo upInfoVo) {

        if(upInfoVo.getOrderNum()==null){
            return  AjaxResult.error("参数错误");
        }

        if(upInfoVo.getLossMoney()==null){
            return AjaxResult.error("参数错误");
        }

        FrontWarrantyInsure sqlInsure = frontWarrantyInsureMapper.selectFrontWarrantyInsureByOrderNum(upInfoVo.getOrderNum());
        if(sqlInsure==null){
            return AjaxResult.error("无法获取该订单信息");
        }

        if(sqlInsure.getReportStatus().equals("2")){
            //修改订单状态
            FrontWarrantyInsure info =new FrontWarrantyInsure();
            info.setWarrantyId(sqlInsure.getWarrantyId());
            info.setReportStatus("4");  //报案状态
            info.setUpdateTime(new Date());
            frontWarrantyInsureMapper.updateFrontWarrantyInsure(info);

            //添加流程
            FrontReportInsureProcess frontReportInsureProcess =new FrontReportInsureProcess();
            frontReportInsureProcess.setOrderNum(upInfoVo.getOrderNum());
            frontReportInsureProcess.setReportStatus("4");
            frontReportInsureProcess.setLossMoney(upInfoVo.getLossMoney());
            frontReportInsureProcess.setLossFiles(upInfoVo.getLossFiles());
            frontReportInsureProcess.setCreateBy(upInfoVo.getCreateBy());
            frontReportInsureProcess.setCreateTime(new Date());
            frontReportInsureProcessMapper.insertFrontReportInsureProcess(frontReportInsureProcess);

            return AjaxResult.success("审核成功");
        }else{
            return AjaxResult.error("还未立案流程");
        }

    }



}
